﻿using AutoMapper;
using MediatR;
using SY.BasicData.Api.Write.Basic.Applications.Cultivation.Command;
using SY.BasicData.Domain;
using SY.BasicData.ErrorCode;
using SY.BasicData.Infrastructures;

namespace SY.BasicData.Api.Write.Basic.Applications.Cultivation.CommandHandler
{
    public class AddPicQueryCommandHandler : IRequestHandler<AddPicQueryCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<PictureModel> picRepository;
        private readonly IMapper mapper;

        public AddPicQueryCommandHandler(IBaseRepository<PictureModel> picRepository, IMapper mapper)
        {
            this.picRepository = picRepository;
            this.mapper = mapper;
        }

        public async Task<ApiResult<int>> Handle(AddPicQueryCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            result.Code = ApiEnum.Success;
            result.Message = "照片添加成功";
            PictureModel pictureModel=mapper.Map<PictureModel>(request);
            result.Data=await picRepository.Add(pictureModel);
            return await Task.FromResult(result);
        }
    }
}
